package com.changgou.util;

import com.changgou.file.FastDfsFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

/**
 * 文件管理工具
 *
 * @author mario
 * @version 1.0
 * @date 2020/11/12 14:24
 */
public class FastDfsClient {

  static {
    // 从classpath下获取文件对象获取路径
    String path = new ClassPathResource("fdfs_client.conf").getPath();
    try {
      ClientGlobal.init(path);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 图片上传
   *
   * @param file 文件
   * @return 文件信息
   */
  public static String[] upload(FastDfsFile file) {
    try {
      TrackerServer trackerServer = getTrackerServer();
      StorageClient storageClient = new StorageClient(trackerServer, null);
      NameValuePair[] metaList =
          new NameValuePair[] {
            new NameValuePair(file.getAuthor()), new NameValuePair(file.getName())
          };

      // 参数1 字节数组
      // 参数2 扩展名(不带点)
      // 参数3 元数据( 文件的大小,文件的作者,文件的创建时间戳)
      String[] strings = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
      return strings;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 图片下载
   *
   * @param groupName 组名
   * @param remoteFileName 文件名
   * @return 获取下载文件
   */
  public static byte[] downFile(String groupName, String remoteFileName) {
    try {
      TrackerServer trackerServer = getTrackerServer();
      StorageClient storageClient = new StorageClient(trackerServer, null);

      return storageClient.download_file(groupName, remoteFileName);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 图片删除
   *
   * @param groupName 组名
   * @param remoteFileName 文件名
   */
  public static Boolean deleteFile(String groupName, String remoteFileName) {
    try {
      TrackerServer trackerServer = getTrackerServer();
      StorageClient storageClient = new StorageClient(trackerServer, null);
      int i = storageClient.delete_file(groupName, remoteFileName);
      Boolean flag = false;
      if (i == 0) {
        flag = true;
      } else {
        flag = false;
      }
      return flag;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据组名获取组的信息
   *
   * @param groupName 组名
   * @return 组
   */
  public static StorageServer getStorages(String groupName) {
    try {
      TrackerClient trackerClient = new TrackerClient();
      TrackerServer trackerServer = trackerClient.getConnection();

      StorageServer group1 = trackerClient.getStoreStorage(trackerServer, groupName);
      return group1;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据文件名和组名获取文件的信息
   *
   * @param groupName 组名
   * @param remoteFileName 文件名
   * @return 文件信息
   */
  public static FileInfo getFile(String groupName, String remoteFileName) {
    try {
      TrackerServer trackerServer = getTrackerServer();
      StorageClient storageClient = new StorageClient(trackerServer, null);

      FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName);
      return fileInfo;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 根据文件名和组名获取组信息的数组信息
   *
   * @param groupName 组名
   * @param remoteFileName 文件名
   * @return 组信息
   */
  public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) {
    try {
      TrackerClient trackerClient = new TrackerClient();
      TrackerServer trackerServer = trackerClient.getConnection();

      ServerInfo[] group1s =
          trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
      return group1s;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 获取tracker的ip和端口的信息
   *
   * @return ip和端口
   */
  public static String getTrackerUrl() {
    try {
      TrackerClient trackerClient = new TrackerClient();
      TrackerServer trackerServer = trackerClient.getConnection();
      String hostString = trackerServer.getInetSocketAddress().getHostString();
      int gTrackerHttpPort = ClientGlobal.getG_tracker_http_port();
      return "http://" + hostString + ":" + gTrackerHttpPort;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 获取tracker
   *
   * @return
   * @throws IOException
   */
  private static TrackerServer getTrackerServer() throws IOException {
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getConnection();
    return trackerServer;
  }
}
